/**
 * @file 工单操作-保修信息
 * @author yangjiangming <449654337@qq.com>
 * @date 2018-12-10 20:13:16
 */
layui.define(['layer', 'form','laytpl','list_modify_common','zmnUtils', '$http'], function (exports) {
    var $ = layui.$;
    var form = layui.form;
    var laytpl = layui.laytpl;
    var opCommonUtils = layui.list_modify_common;
    var zmnUtils = layui.zmnUtils;
    var $http = layui.$http;

    /**
     * 禁用所有输入
     */
    function disableAll(who) {
        if (who) {
            $(who).parents('form').find(':input').not('.layui-btn').prop('disabled', true);
        } else {
            $(':input').not('.layui-btn').prop('disabled', true);
        }
        form.render();
    }

    /**
     * 启用所有输入
     */
    function enableAll(who) {
        $(who).parents('form').find(':input').not('.layui-btn').prop('disabled', false);
        form.render();
    }


    function tpl() {
        var getTpl = card.innerHTML;
        laytpl(getTpl).render(orderWarrantyVO, function(html){
            $('#view').html(html);
        });
    }

    function addDayEvent() {
        //监听预付款框
        form.verify({
            "day-number": function (val) {
                if (val != null && val !== '') {
                    if (!isNumber(val)) {
                        return "请输入正整数"
                    }
                    if (val < orderWarrantyVO.minDay) {
                        return "不能小于" + orderWarrantyVO.minDay + "天"
                    }
                    if (val > 730) {
                        return "不能大于730天"
                    }
                }
            }
        })
    }

    /**
     * 判断是否是正整数
     * @param val {number/string} 字符串或数字
     * @returns {boolean} 返回是否是数字
     */
    function isNumber(val) {
        var reg = /^[1-9]\d*$/;//正整数
        return reg.test(val);
    }

    function btnEvent() {
        var viewData = [];
        form.on('submit(modify)', function (data) {
            var warrantyCardIndex = $(this).parents('form').attr('cols');
            viewData[warrantyCardIndex] = {'html': $(this).parents('form').html(), 'alreadySaved': false};
            enableAll(this);
            var $par = $(this).parent();
            $par.find('.modify-btn').hide();
            $par.find('.delete-btn').hide();
            $par.find('.save-btn').show();
            $par.find('.cancel-btn').show();
            if (orderWarrantyVO.modifyDay) {
                $par.parent().parent().siblings().find('.modify-add-day').show();
                $par.parent().parent().siblings().find('.modify-day').hide();
            }
            return false;
        });

        form.on('submit(cancel)', function (data) {
            var warrantyCardIndex = $(this).parents('form').attr('cols');
            if (!viewData[warrantyCardIndex].alreadySaved) {
                $(this).parents('form').empty().html(viewData[warrantyCardIndex].html);
                form.render();
                return false;
            }
            var $par = $(this).parent();
            $par.find('.modify-btn').show();
            $par.find('.delete-btn').show();
            $par.find('.save-btn').hide();
            $par.find('.cancel-btn').hide();
            if (orderWarrantyVO.modifyDay) {
                $par.parent().parent().siblings().find('.modify-add-day').hide();
                $par.parent().parent().siblings().find('.modify-day').show();
            }
            disableAll(this);
            return false;
        });
        form.on('submit(delete)', function (data) {
            var dto = {
                orderId:data.field.orderId,
                proExtId:data.field.proExtId
            };
            var url = "/work/modify/delete/warranty.action";
            $http.save(url, dto, data.elem).then(function () {
                var $form = $(data.form);
                $form.find(':checked').removeAttr('checked');
                $form.find('textarea').val('');
                form.render();
                $form.hide();
            }, (msg) => {
                layer.msg(msg);
            });
            return false;
        });

        form.on('submit(save)', function (data) {
            data.field.fault = [];
            $(data.form).find(':checkbox:checked').each(function () {
                data.field.fault.push({
                    mapId: this.value,
                    mapName: this.getAttribute('title')
                });

            });
            data.field['productName'] = $(data.form).find('.productName').text();

            if ((data.field.fault == null || data.field.fault.length === 0) && (data.field.remark.trim() === '' || data.field.remark == null)) {
                layer.msg("保修范围或其他保修至少填一个");
                return false;
            }

            if (zmnUtil.isEmptyNull(data.field.day) && zmnUtil.isEmptyNull(data.field.addDay)) {
                layer.msg("保修天数不能为空");
                return false;
            }

             var url = "/work/modify/save/warranty.action";
            $http.lockSave(url, data.field, data.elem).then(function () {
                layer.msg('操作成功', { icon: 1, time: 1000 }, function () {
                    opCommonUtils.goOperateHome();
                });
            }, (msg) => {
                layer.msg(msg);
            });

            return false;
        });

        form.on('submit(addition)', function () {
            var  $hideForm=$('form:hidden');
            if ($hideForm.length === 0) {
                layer.msg('订单内容都已添加，不能增加以外的保修');
                return;
            }
            $hideForm.show();
        });
    }

    function radioEvent() {
        form.on('radio(month)', function(data){
            var dateStr = $(data.elem).parents('form').find('.startTime').text();
            if (dateStr) {
                var startDate = new Date(Date.parse(dateStr.replace('年', '-').replace('月', '-').replace('日', '')));
                startDate.setMonth(startDate.getMonth() + (+this.value));
                var year = startDate.getFullYear();
                var month = startDate.getMonth() + 1;
                var day = startDate.getDate();
                if (month < 10) {
                    month = "0" + month;
                }
                if (day < 10) {
                    day = "0" + day;
                }
                var endDate = year + "年" + month + "月" + day + "日";
                $(data.elem).parents('form').find('.endTime').text(endDate);
            }
        });
    }

    /**
     * 初始化页面
     */
    function initPage() {
        tpl();
        disableAll();
    }

    /**
     * 初始化页面事件
     */
    function initEvent() {
        btnEvent();
        radioEvent();
        addDayEvent();
    }

    // 程序入口
    function init() {

        // 初始化页面
        initPage();

        // 初始化事件
        initEvent();

    }


    // 初始化
    init();

    exports('oms_work_modify_warranty');
});
